package com.tjsinfo.tjsoa.workplan.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tjsinfo.tjsoa.common.TestRiQi;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.workplan.bo.IWorkPlanExamineBo;
import com.tjsinfo.tjsoa.workplan.bo.IWorkPlanMainBo;
import com.tjsinfo.tjsoa.workplan.dao.IWorkPlanUserDAO;
import com.tjsinfo.tjsoa.workplan.sqlmap.UserInfo;
import com.tjsinfo.tjsoa.workplan.sqlmap.WorkPlanDefine;

/**
 * 添加工作计划
 * @author wangxiaowei
 *
 */
public class AddWorkPlanAction extends Action{

	private IWorkPlanMainBo planMainBo;
	private IWorkPlanExamineBo workPlanExamineBo;
	private IWorkPlanUserDAO workPlanUserDAO;
	
	
	public IWorkPlanMainBo getPlanMainBo() {
		return planMainBo;
	}


	public void setPlanMainBo(IWorkPlanMainBo planMainBo) {
		this.planMainBo = planMainBo;
	}


	public IWorkPlanExamineBo getWorkPlanExamineBo() {
		return workPlanExamineBo;
	}


	public void setWorkPlanExamineBo(IWorkPlanExamineBo workPlanExamineBo) {
		this.workPlanExamineBo = workPlanExamineBo;
	}


	public IWorkPlanUserDAO getWorkPlanUserDAO() {
		return workPlanUserDAO;
	}


	public void setWorkPlanUserDAO(IWorkPlanUserDAO workPlanUserDAO) {
		this.workPlanUserDAO = workPlanUserDAO;
	}


	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		// TODO Auto-generated method stub
		try {
			String time = request.getParameter("time");
			if (time == null || "".equals(time)) {
				date = new Date();
			} else {
				date = dateFormat.parse(time);	
			}
			
			Integer submittype = 0;
			String shangbaoren = request.getParameter("jubaorenid");
			if(shangbaoren != null&&!"".equals(shangbaoren))
			{
				shangbaoren=shangbaoren.substring(0,shangbaoren.length() - 1);
				submittype = 1;
			}
			WorkPlanDefine workPlanDefine = new WorkPlanDefine();
			workPlanDefine.setTjsUserByUserId(user.getUser_id());
			workPlanDefine.setType("D");
			List list =  workPlanUserDAO.selectDefine(workPlanDefine);
			if (list.size() == 0) {
				submittype = 3;
			}
			String jihuaid =  planMainBo.save(user.getUser_id(), date, request.getParameter("type"), request.getParameter("contest"), "",Integer.parseInt(request.getParameter("year")),Integer.parseInt(request.getParameter("number")),1,submittype);
			workPlanExamineBo.addShangBaoRen(shangbaoren, jihuaid);
			if("D".equals(request.getParameter("type")))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/workPlanDateModle.do");
				request.setAttribute("message", "workplan.message.save");
				return mapping.findForward("success");
				
			}
			else if("W".equals(request.getParameter("type")))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/workPlanWeekModle.do");
				request.setAttribute("message", "workplan.message.save");
				return mapping.findForward("success");
				
			}
			else if("M".equals(request.getParameter("type")))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/workPlanMonthModle.do");
				request.setAttribute("message", "workplan.message.save");
				return mapping.findForward("success");
			}
			else if("Q".equals(request.getParameter("type")))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/workPlanJiDuModle.do");
				request.setAttribute("message", "workplan.message.save");
				return mapping.findForward("success");
				
			}
			else if("H".equals(request.getParameter("type")))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/workPlanHalfYearModle.do");
				request.setAttribute("message", "workplan.message.save");
				return mapping.findForward("success");
			}
			else if("Y".equals(request.getParameter("type")))
			{
				request.setAttribute("linkurl", "tjsinfo/tjsoa/workplan/workPlanYearModle.do");
				request.setAttribute("message", "workplan.message.save");
				return mapping.findForward("success");
			}	
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return mapping.findForward("error");
	}

}
